home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE20 / CLINIC / PORT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-02-26  |  1.4 KB  |  71 lines

  1. unit Port;
  2.  
  3. { Written by Andrew Clark, andyc@rmpd-ngh.demon.co.uk }
  4.  
  5. interface
  6.  
  7. uses
  8.  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  9.  Dialogs;
  10.  
  11. type
  12.  
  13.   EPortError = class(Exception);
  14.  
  15.   TPort = class(TComponent)
  16.   private
  17.     { Private declarations }
  18.     procedure Outport(Address,Data:Word);
  19.     function  InPort(Address:Word):Word;
  20.   protected
  21.     { Protected declarations }
  22.   public
  23.     { Public declarations }
  24.     constructor Create(AOwner:TComponent);override;
  25.     property    Port[index:Word]:Word read InPort write OutPort;default;
  26.   published
  27.     { Published declarations }
  28.   end;
  29.  
  30. procedure Register;
  31.  
  32. implementation
  33.  
  34. procedure TPort.OutPort(Address,Data:Word);
  35. asm
  36.   mov dx,Address
  37.   mov ax,Data
  38.   out dx,ax
  39. end;
  40.  
  41. function TPort.InPort(Address:Word):Word;
  42. asm
  43.  mov dx,Address
  44.  in ax,dx
  45. end;
  46.  
  47. constructor TPort.Create(AOwner:TComponent);
  48. var
  49.  h:Integer;
  50. begin
  51.  Inherited Create(AOwner);
  52.  Inherited Create(AOwner);
  53.  { load the driver }
  54.  h:=CreateFile('\\.\\giveio',GENERIC_READ,0,nil,
  55.                OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  56.  
  57.  { warn if driver not there }
  58.  if H=INVALID_HANDLE_VALUE then
  59.   raise EPortError.Create('GiveIO Driver Not Installed');
  60.  
  61.  { we don't actually need the handle, as loading the driver
  62.    enables the free port access for the period of the program }
  63.  CloseHandle(h);
  64. end;
  65.  
  66. procedure Register;
  67. begin
  68.   RegisterComponents('AndyC', [TPort]);
  69. end;
  70.  
  71. end.